/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author benbryan
 */
public class PropertiesManager {
    private static final File propertiesFile = new File("AVM_Server.properties");
    private static final String HOSTS_KEY = "HOSTS";
    private static final String KV_STORE_NAME_KEY = "KV_STORE_NAME";
    private static final String USER_SESSION_TIMEOUT_MINUTES = "USER_SESSION_TIMEOUT_MINUTES";
    private static final String UPLOAD_SESSION_TIMEOUT_MINUTES = "UPLOAD_SESSION_TIMEOUT_MINUTES";
        
    public static Properties getDefaultProperties(){
        Properties p = new Properties();
        p.setProperty(HOSTS_KEY, "localhost:5000");
        p.setProperty(KV_STORE_NAME_KEY, "kvstore");
        p.setProperty(USER_SESSION_TIMEOUT_MINUTES, "30");
        p.setProperty(UPLOAD_SESSION_TIMEOUT_MINUTES, "120");
        return p;
    }

    public static int getUploadTimeoutMinutes(){
        Properties p = getProperties();
        String s = p.getProperty(UPLOAD_SESSION_TIMEOUT_MINUTES);
        if (s == null){
            s = getDefaultProperties().getProperty(UPLOAD_SESSION_TIMEOUT_MINUTES);
            p.setProperty(UPLOAD_SESSION_TIMEOUT_MINUTES, s);
            setProperties(p);
        } 
        return Integer.valueOf(s);
    }
    public static int getUserSessionTimeoutMinutes(){
        Properties p = getProperties();
        String s = p.getProperty(USER_SESSION_TIMEOUT_MINUTES);
        if (s == null){
            s = getDefaultProperties().getProperty(USER_SESSION_TIMEOUT_MINUTES);
            p.setProperty(USER_SESSION_TIMEOUT_MINUTES, s);
            setProperties(p);
        } 
        return Integer.valueOf(s);
    }
    public static String[] getHosts(){
        // if in _Dev, then host ports will be incremented by 1. 
        Properties p = getProperties();
        String s = p.getProperty(HOSTS_KEY);
        if (s == null){
            s = getDefaultProperties().getProperty(HOSTS_KEY);
            p.setProperty(HOSTS_KEY, s);
            setProperties(p);
        }
        String[] hosts = s.split(",");
        return hosts;
    }
    public static String getKV_StoreName(){
        Properties p = getProperties();
        String s = p.getProperty(KV_STORE_NAME_KEY);
        if (s == null){
            s = getDefaultProperties().getProperty(KV_STORE_NAME_KEY);
            p.setProperty(KV_STORE_NAME_KEY, s);
            setProperties(p);
        } 
        return s;
    }
    
    
    public static Properties getProperties(){
        try {
            if (!propertiesFile.exists()){
                propertiesFile.createNewFile();
            }
            InputStream is = new FileInputStream(propertiesFile);
            Properties p = new Properties();
            p.load(is);
            return p;
        } catch (IOException ex) {
            Logger.getLogger(PropertiesManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public static void setProperties(Properties p){
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(propertiesFile);
            p.store(fos, null);
        } catch (IOException ex) {
            Logger.getLogger(PropertiesManager.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(PropertiesManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
    public static File getPropertiesFile() {
        return propertiesFile;
    }
    
}
